package botcraft.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Launcher extends GuiApplication {
	private static final String BACKGROUND = "/botcraft.png";
	private static final Logger LOGGER = Logger.getLogger(Launcher.class.getName());
	
	public static void main(final String[] arguments) {
		try {
			GuiApplication.invoke(new Launcher());
		} catch (final InterruptedException e) {
		} catch (final InvocationTargetException e) {
			LOGGER.log(Level.SEVERE, "Error invoking launcher", e);
			JOptionPane.showMessageDialog(null, "Error invoking launcher", e.getMessage(), JOptionPane.ERROR_MESSAGE);
		}
	}
	
	@Override
	public void run() {
		final JFrame frame = new JFrame(getResourceBundle().getString("botcraftLauncher"));
		try {
			final Image background = ImageIO.read(Launcher.class.getResource(BACKGROUND));
			final JPanel contentPane = new JPanel() {
				private static final long serialVersionUID = 0L;
	
				@Override
				protected void paintComponent(final Graphics graphics) {
					graphics.drawImage(background, 0, 0, Color.BLACK, null);
				}
			};
			final Dimension size = new Dimension(background.getWidth(null), background.getHeight(null));
			contentPane.setMinimumSize(size);
			contentPane.setMaximumSize(size);
			contentPane.setPreferredSize(size);
			frame.setContentPane(contentPane);
			frame.setResizable(false);
		} catch (final IOException e) {
			LOGGER.log(Level.WARNING, "Error loading background image", e);
		}
		frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
		final JButton newGame = new JButton(getResourceBundle().getString("newGame"));
		newGame.setAlignmentX(Component.CENTER_ALIGNMENT);
		frame.add(newGame);
		final JButton settings = new JButton(getResourceBundle().getString("settings"));
		settings.setAlignmentX(Component.CENTER_ALIGNMENT);
		frame.add(settings);
		final JButton about = new JButton(getResourceBundle().getString("about"));
		about.setAlignmentX(Component.CENTER_ALIGNMENT);
		frame.add(about);
		final JButton exit = new JButton(getResourceBundle().getString("exit"));
		exit.setAlignmentX(Component.CENTER_ALIGNMENT);
		frame.add(exit);
		frame.pack();
		newGame.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent actionEvent) {
				
			}
		});
		settings.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent actionEvent) {
				SwingUtilities.invokeLater(new Settings());
			}
		});
		about.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent actionEvent) {
				try {
					SwingUtilities.invokeLater(new About());
				} catch (final IOException e) {
					LOGGER.log(Level.WARNING, "Error loading about", e);
					JOptionPane.showMessageDialog(frame, "Error loding about", e.getMessage(), JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		exit.addActionListener(new ActionListener () {
			@Override
			public void actionPerformed(final ActionEvent actionEvent) {
				frame.dispose();
			}
		});
		frame.setVisible(true);
		frame.requestFocusInWindow();
	}
}
